package com.getsendsocial.GetSendSocial.Activity;


import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;

import com.getsendsocial.GetSendSocial.R;
import com.getsendsocial.GetSendSocial.Facebook.AbstractAsyncActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FacebookActivity extends AbstractAsyncActivity {

	protected static final String TAG = FacebookActivity.class.getSimpleName();

	private ConnectionRepository connectionRepository;

	private FacebookConnectionFactory connectionFactory;

	/**
	 * Called when the activity is first created.
	 * @param savedInstanceState If the activity is being re-initialized after 
	 * previously being shut down then this Bundle contains the data it most 
	 * recently supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it is null.</b>
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.facebook_activity);
		this.connectionRepository = getApplicationContext().getConnectionRepository();
		this.connectionFactory = getApplicationContext().getFacebookConnectionFactory();
	}

	@Override
	public void onStart() {
		super.onStart();
		if (isConnected()) {
			System.out.println("Connected");
			//showFacebookOptions();
		} else {
			showConnectOption();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	// ***************************************
	// Private methods
	// ***************************************
	private boolean isConnected() {
		return connectionRepository.findPrimaryConnection(Facebook.class) != null;
	}

	private void disconnect() {
		this.connectionRepository.removeConnections(this.connectionFactory.getProviderId());
	}

	private void showConnectOption() {
		String[] options = { "Connect" };
		Button b = (Button) findViewById(R.id.buttonConnectFacebook);
		b.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				displayFacebookAuthorization();
				
			}
		});
	}

//	private void showFacebookOptions() {
//		String[] options = { "Disconnect", "Profile", "Home Feed", "Wall Post" };
//		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options);
//		ListView listView = (ListView) this.findViewById(R.id.facebook_activity_options_list);
//		listView.setAdapter(arrayAdapter);
//
//		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//			public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) {
//				Intent intent;
//				switch (position) {
//				case 0:
//					disconnect();
//					showConnectOption();
//					break;
//				case 1:
//					intent = new Intent();
//					intent.setClass(parentView.getContext(), FacebookProfileActivity.class);
//					startActivity(intent);
//					break;
//				case 2:
//					intent = new Intent();
//					intent.setClass(parentView.getContext(), FacebookHomeFeedActivity.class);
//					startActivity(intent);
//					break;
//				case 3:
//					intent = new Intent();
//					intent.setClass(parentView.getContext(), FacebookWallPostActivity.class);
//					startActivity(intent);
//					break;
//				default:
//					break;
//				}
//			}
//		});
//	}

	private void displayFacebookAuthorization() {
		Intent intent = new Intent();
		intent.setClass(this, FacebookWebOAuthActivity.class);
		startActivity(intent);
		finish();
	}



}

